package sample3.provider;

import sample3.provider.ddl.SqliteTableBuilder;
import sample3.provider.ddl.SqliteViewBuilder;
import android.net.Uri;
import android.provider.BaseColumns;

/**
 * @author YUN
 */
public interface MyDatabase {
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "sample2.provider.MyDatabase.db";
    public static final String AUTHORITY = "sample2.provider.MyDatabase";
    public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/");

    public final static class MyTable extends SqliteTableBuilder {
        static final String DIR_TYPE = "vnd.android.cursor.dir/vnd.reco.MyTable";
        static final String ITEM_TYPE = "vnd.android.cursor.item/vnd.reco.MyTable";
        static final String NAME = "MyTable";

        public static final Uri CONTENT_URI = Uri.withAppendedPath(MyDatabase.BASE_CONTENT_URI, NAME);

        public MyTable() {
            super(NAME);
            //
            addIntegerColumn(Columns._ID, CONSTRAINT_PRIMARY_KEY_AUTOINCREMENT_NOT_NULL);
            addTextColumn(Columns.NAME, CONSTRAINT_NOT_NULL);
            addIntegerColumn(Columns.AGE);
        }

        public static interface Columns extends BaseColumns {
            /** type:Text */
            public static final String NAME = "name";
            /** type:Integer */
            public static final String AGE = "age";
        }
    }

    public final class MyView extends SqliteViewBuilder {
        static final String DIR_TYPE = "vnd.android.cursor.dir/vnd.reco.MyView";
        static final String ITEM_TYPE = "vnd.android.cursor.item/vnd.reco.MyView";
        static final String NAME = "MyView";

        public static final Uri CONTENT_URI = Uri.withAppendedPath(MyDatabase.BASE_CONTENT_URI, NAME);

        public static interface Columns extends MyTable.Columns {
            /** type:Text */
            public static final String NAME = "name";
        }

        public MyView() {
            super(NAME);
            addSelect(Columns._ID, "sub.%s", Columns._ID);
            addSelect(Columns.NAME, "sub.%s", Columns.NAME);
            setFrom("(SELECT %s,%s FROM %s " +
                    "UNION ALL " +
                    "SELECT -1, SUBSTR(%s,1,1) section FROM %s GROUP BY section) sub",
                    Columns._ID,
                    Columns.NAME,
                    MyTable.NAME,
                    Columns.NAME,
                    MyTable.NAME);
            setOrderBy(Columns.NAME);
        }
    }
}
